package com.exploadingelephants.sprite;

import java.io.Serializable;
import java.util.Random;

import android.graphics.BitmapFactory;

import com.exploadingelephants.R;
import com.exploadingelephants.util.Settings;
import com.exploadingelephants.views.PlaygroundView;

/**
 * Definition of an Elephant
 */
public class Elephant extends Sprite implements Serializable {
    private static final long serialVersionUID = 8782766305424408733L;

    /**
     * @param playView
     * @param material
     */
    public Elephant(PlaygroundView playgroundView, long id) {
	super(playgroundView, id);
	setImage();
	setDefaultMovement();
    }

    @Override
    public void setDefaultMovement() {
	// Random movement
	Random rnd = new Random();
	int xPos = rnd.nextInt((int) parentView.getWidth()) - Settings.MAX_SPEED;
	int yPos = rnd.nextInt((int) parentView.getHeight()) - Settings.MAX_SPEED;
	int xSpeed = rnd.nextInt(Settings.MAX_SPEED * 2) - Settings.MAX_SPEED;
	int ySpeed = rnd.nextInt(Settings.MAX_SPEED * 2) - Settings.MAX_SPEED;

	movement = new Movement(xPos, yPos, xSpeed, ySpeed);
    }

    @Override
    protected void update() {
	// Collides on left or right side of border
	if (movement.getxPosition() >= parentView.getWidth() - width
		- movement.getxSpeed()
		|| movement.getxPosition() + movement.getxSpeed() <= 0) {
	    movement.setxSpeed(-movement.getxSpeed());
	}
	// Collides on top or bottom side of border
	if (movement.getyPosition() >= parentView.getHeight() - height
		- movement.getySpeed()
		|| movement.getyPosition() + movement.getySpeed() <= 0) {
	    movement.setySpeed(-movement.getySpeed());
	}
	movement.setxPosition(movement.getxPosition() + movement.getxSpeed());

	movement.setyPosition(movement.getyPosition() + movement.getySpeed());
    }

    @Override
    protected void setImage() {
	bmp = BitmapFactory.decodeResource(parentView.getResources(),
		R.drawable.elephant);
	width = bmp.getWidth();
	height = bmp.getHeight();
    }

    /**
     * exploding elephant
     */
    public void explode() {
	bmp = BitmapFactory.decodeResource(parentView.getResources(),
		R.drawable.explosion);
    }
}
